<!DOCTYPE html>
<html>
<head>
    <title>分类管理</title>
    <#include "/header.html">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-btn-container">
            <button class="layui-btn" id="btn-expand">全部展开</button>
            <button class="layui-btn layui-bg-black" id="btn-fold">全部折叠</button>
            <#if shiro.hasPermission("app:category:save")>
            <button class="layui-btn layui-btn-normal" id="btn-add"><i class="layui-icon layui-icon-add-circle"></i>添加分类</button>
            </#if>
        </div>
        <table class="layui-table" id="currentTableId"></table>
    </div>
</div>

<script type="text/html" id="currentTableTagTpl">
    {{# layui.each(d.keyword, function(index, item){ }}
    <span class="layui-badge-rim">{{item}}</span>
    {{# }); }}
</script>

<script type="text/html" id="currentTableStatusTpl">
    {{# if(d.status == 1){ }}
    <span class="layui-badge layui-bg-green">启用</span>
    {{# } else { }}
    <span class="layui-badge layui-bg-grey">禁用</span>
    {{# } }}
</script>

<script type="text/html" id="imgtemp">
	<div><a href="{{d.image}}" target="_blank"><img src="{{d.image}}" class="layui-nav-img" style="width:20px;height:20px"></a></div>
</script>
<script type="text/html" id="currentTableBar">
    {{# if(d.id !== 1){ }}
    <#if shiro.hasPermission("app:category:save")>
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    </#if>
    <#if shiro.hasPermission("app:category:delete")>
    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </#if>
    {{# } }}
</script>

<script type="text/javascript">
    var formDataId = 0;
    var menuTreeData = [];
    var table;

    function loadMenuData() {
        menuTreeData = [];
        $.ajax({
            type: "POST",
            url: serverUrl('category/tree'),
            data: {},
            dataType: "json",
            success: function (result) {
                menuTreeData = result;
            }
        });
    }

    $(document).ready(function () {
        loadMenuData();
    });

    layui.use(['treeTable'], function () {
        var $ = layui.jquery,
            treeTable = layui.treeTable;

        layer.load(2);
        // 渲染树形表格
        table = treeTable.render({
            elem: '#currentTableId',
            url: serverUrl('/category/list'),
            tree: {
                iconIndex: 2,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'parentId'     // pid字段名称
            },
            cols: [[
                {type: 'numbers', width: 80},
                {field: 'image', title: '图片', width: 100, templet: '#imgtemp'},
                {field: 'name', minWidth: 150, title: '名称'},
                {
                    field: 'grade', width: 80, align: 'center', templet: function (d) {
                        if (d.grade === 0) {
                            return '<span class="layui-badge layui-bg-gray">顶级</span>';
                        }
                        if (d.grade === 1) {
                            return '<span class="layui-badge-rim">一级</span>';
                        }
                        if (d.grade === 2) {
                            return '<span class="layui-badge-rim">二级</span>';
                        }
                        if (d.grade === 3) {
                            return '<span class="layui-badge-rim">三级</span>';
                        }
                    },
                    title: '层级'
                },
                {field: 'identifier', minWidth: 150, title: '识别码'},
                {field: 'sort', minWidth: 150, title: '排序值'},
                {field: 'remark', minWidth: 120, title: '备注'},
                {field: 'createTime', minWidth: 120, title: '创建时间'},
                {field: 'status', title: '状态', minWidth: 80, templet: '#currentTableStatusTpl'},
                {templet: '#currentTableBar', width: 150, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        //添加按钮
        $('#btn-add').click(function () {
            formDataId = 0;
            openFormView('app/category/form.html', '添加分类');
        });
        //展开按钮
        $('#btn-expand').click(function () {
            table.expandAll();
        });
        //折叠按钮
        $('#btn-fold').click(function () {
            table.foldAll();
        });

        treeTable.on('tool(currentTableId)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                formDataId = data.id;
                openFormView('app/category/form.html', '编辑分类');
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除 '+data.name+' 吗？', function (index) {
                    deleteTableRow('category/delete/', [data.id], function (rs) {
                        table.reload();
                    });
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>